<template>
    <div class="container containerStudent">
        <!-- header -->
        <div class="header">
            <div class="header-box">
                <p class="header-title">上级管培生:<span v-if="user.phone !== '' ">{{ user.true_name }}-{{ user.phone }}</span></p>
            </div>
            <div class="header-box">
                <p class="header-title">姓名:</p>
                <el-input
                    v-model="header.tel"
                    placeholder="请输入姓名"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    class="search-ipt"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-box">
                <p class="header-title">用户:</p>
                <el-input
                    v-model="header.keyword_student_id"
                    placeholder="用户ID或手机号"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    class="search-ipt"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="header-box">
                <p class="header-title">是否购买:</p>
                <el-select
                    v-model="header.purchase"
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                    clearable
                >
                    <el-option
                        v-for="item in header.purchaseList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="header-box">
                <p class="header-title">是否班级认证:</p>
                <el-select
                    v-model="header.class_verify"
                    placeholder="全部"
                    size="small"
                    @change="getTableData(1)"
                >
                    <el-option
                        label="全部"
                        :value="99"
                    />
                    <el-option
                        label="是"
                        :value="1"
                    />
                    <el-option
                        label="否"
                        :value="0"
                    />
                </el-select>
            </div>
            <div class="header-box">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnFormReset()"
                >
                    重置
                </el-button>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="goodsTable"
            @select="fnSelect"
            @select-all="fnSelectAllTop"
            @selection-change="handleSelectionChange"
        >
            <el-table-column
                type="selection"
                width="60"
                align="center"
                fixed="left"
            />
            <el-table-column
                label="序号"
                width="60"
                align="center"
                type="index"
                fixed="left"
            />
            <el-table-column
                label="用户信息"
                width="220"
                fixed="left"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="cell-title">
                        <div class="user-name">{{ scope.row.true_name ? scope.row.true_name : '--' }}</div>
                        <div class="user-tags">
                            <div class="tag-item">
                                <img
                                    src="@/assets/image/finance/class-light.png"
                                    v-if="scope.row.class_verify"
                                >
                                <img
                                    src="@/assets/image/finance/class.png"
                                    v-else
                                >
                            </div>
                        </div>
                    </div>
                    <div class="cell-tips">手机号：{{ scope.row.phone }}</div>
                    <div class="cell-tips">用户ID：{{ scope.row.student_no }}</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="school_name"
                label="学校名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="school_college_name"
                label="学院名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="join_time"
                label="加入时间"
                :width="140"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.join_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="is_buy"
                label="是否购买"
                align="center"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.is_buy == 1">是</span>
                    <span v-if="scope.row.is_buy == 0">否</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="buy_num"
                label="购买次数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="购买记录"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        :disabled="scope.row.buy_num == 0"
                        @click="fnGetbuyList(scope.row, 1)"
                    >
                        查看
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                prop="person_money"
                label="个人消费金额"
                show-overflow-tooltip
                :width="140"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.person_money | setPrice }}元</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="rebate"
                label="预估佣金"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.rebate | setPrice }}元</span>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 查看购买记录 -->
        <el-dialog
            :visible.sync="DialogVisibleList"
            width="760px"
            class="seckillDialogVisible"
        >
            <el-table
                :data="tableDataReord"
                max-height="500px"
                style="width: 100%;"
                border
            >
                <el-table-column
                    label="序号"
                    type="index"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="create_time"
                    label="下单日期"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="aim_type"
                    label="商品类型"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.aim_type | setAimtype }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="ori_money"
                    label="实付款"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.ori_money }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="rebate_money"
                    label="产生佣金"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.rebate_money }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="status"
                    label="订单状态"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.status | setStatus }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="fnJumpShop(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="padding: 15px 0">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page.sync="pageOrder.now_page"
                    :total="pageOrder.total_count"
                    :page-size="pageOrder.page_size"
                    @current-change="fnGetbuyList"
                    style="float: right"
                />
            </div>
        </el-dialog>
        <!-- /查看购买记录 -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-checkbox
                v-model="checkedAll"
                :indeterminate="checkIndeterminate"
                @change="fnChangeChecked"
            >全选</el-checkbox>
            <el-button
                type="text"
                @click="fnDeleteFans()"
                :disabled="multipleSelection.length == 0 ? true : false"
            >删除</el-button>
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { setDate } from '@/assets/js/utils';
export default {
    /**
     * @param  {Array}     tableData            //表单数据源
     * @param  {Object}    page                 //分页
     * @param  {String}    tel                  //手机号码
     * @param  {Array}     date                 //选择时间
     * @param  {Number}    purchase             //考核结果
     * @param  {Array}     purchaseList         //考核结果数据
     * @param  {Array}     tableDataReord       //购买记录数据
     * @param  {Boolean}   DialogVisibleList    //购买记录数据弹窗
     * @param  {Boolean}   dialogVisibleFp      //分配弹窗开关
     * @param  {Object}    ruleForm             //分配弹窗字段
     * @param  {Array}     dynamicTags          //搜索管培生数据
     * @param  {Array}     restaurants          //管培生数据
     * @param  {Object}    user                 //上级管培生数据
     */
    data () {
        return {
            student_no: '',//存储弹窗数据编号
            checkIndeterminate: false,
            multipleSelection: [],
            checkedAll: false,
            user: {
                true_name: '',
                phone: ''
            },
            dynamicTags: [],
            restaurants: [],
            ruleForm: {
                phone: ''
            },
            dialogVisibleFp: false,
            DialogVisibleList: false,
            tableDataReord: [],
            header: {
                date: [],
                tel: '',
                keyword_student_id: '',
                purchase: 0,
                class_verify: 99,
                purchaseList: [{
                    value: 0,
                    label: '全部'
                },{
                    value: 1,
                    label: '是'
                },{
                    value: -1,
                    label: '否'
                }]
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            pageOrder: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {},
            agent_no: ''
        };
    },
    filters: {
        setPrice (val) {
            return val.toFixed(2);
        },
        setAimtype (val) {
            if (val == 1) {
                return '自营商品订单';
            } else if (val == 2) {
                return '自营店铺';
            } else if (val == 3) {
                return '非自营店铺';
            }
        },
        setStatus (val) {
            if (val == 0) {
                return '已付款';
            } else if (val == 1) {
                return '返利待入账';
            } else if (val == 2) {
                return '返利已入账';
            } else if (val == 3) {
                return '已退款';
            }
        }
    },
    watch: {
        DialogVisibleList (val) {
            if (!val) {
                this.tableDataReord = [];
            }
        }
    },
    created () {
        let query = this.$route.query;

        if (query.phone !== undefined || query.true_name !== undefined || query.purchase !== undefined || query.agent_no !== undefined) {
            this.header.purchase = query.purchase;
            this.agent_no = query.agent_no;
            this.user = {
                true_name: query.true_name,
                phone: query.phone
            };
        }

        this.authResult = this.$route.meta.authResult;
        this.setHeader();
        this.getTableData();
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '管培生'
            },
            {
                title: "管培生粉丝"
            },{
                title: "二级粉丝"
            }]
            );
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/stuagent_fans%5Cget_fans_list', {
                page_id: page || this.page.now_page,
                agent_no: this.agent_no,
                fans_keyword: this.header.tel,
                is_buy: this.header.purchase,
                keyword_student_id: this.header.keyword_student_id,
                class_verify: this.header.class_verify
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 重置
         */
        fnFormReset () {
            this.header.tel = this.header.purchase = this.header.date = '';
            this.getTableData();
        },

        /**
         * 查看购买记录
         */
        fnGetbuyList (row, page) {
            //查看操作执行
            if(row.student_no !== undefined) {
                this.student_no = row.student_no;
                this.DialogVisibleList = !this.DialogVisibleList;
            }

            this.$post('/student/stuagent_fans%5Cget_buy_list',{
                student_no: this.student_no,
                page_id: page || this.pageOrder.now_page
            },resp=>{
                if (resp.code == 1) {
                    this.tableDataReord = resp.data.list;
                    this.pageOrder.now_page = resp.data.page.now_page;
                    this.pageOrder.total_count = resp.data.page.total_count;
                    this.pageOrder.page_size = resp.data.page.page_size;
                    // this.tableDataReord = this.tableDataReord.concat(resp.data.list);
                    // this.DialogVisibleList = !this.DialogVisibleList;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 跳转订单列表
         */
        fnJumpShop (row) {
            let data = {};

            if(row.aim_type == 1) {
                data = {
                    aim_no: row.aim_no,
                    fans: 0,
                    shop_type: 0
                };
            } else {
                data = {
                    aim_no: row.aim_no,
                    fans: 0,
                    shop_type: 2
                };
            }

            let routeData = this.$router.resolve({
                name: 'OrderList',
                query: {
                    ...data
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 删除粉丝
         */
        fnDeleteFans () {
            let student = '';

            this.multipleSelection.map(item=>{
                student += item.student_no + ',';
            });

            this.$confirm('此操作将批量删除选中粉丝, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent%5Cdelete_fans',{
                    student_no: student
                },resp=>{
                    if (resp.code == 1) {
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                        setTimeout(()=>{
                            this.getTableData();
                        },200);
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 选择Checkbox
         */
        fnSelect(selection) {
            if(selection.length == this.tableData.length) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }
            if(selection.length > 0 && selection.length < this.tableData.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        fnSelectAllTop(selection) {
            if(selection.length > 0) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }
            if(selection.length > 0 && selection.length < this.tableData.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        fnChangeChecked() {
            if(this.checkedAll){
                this.toggleSelection(1);
            }else{
                this.toggleSelection();
            }
        },
        toggleSelection(rows) {
            if (rows) {
                this.$refs.goodsTable.toggleAllSelection();
            } else {
                this.$refs.goodsTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .header-box {
                display: flex;
                margin-right: 20px;
                align-items: center;
                color: #606266;
                margin-bottom: 20px;
                .header-title {
                    margin-right: 10px;
                    white-space: nowrap;
                }
                .btn-import-wrap {
                    margin-left: 15px;
                    display: inline-block;
                    position: relative;
                    overflow: hidden;
                    cursor: pointer;
                    vertical-align: top;
                    .import-input {
                        margin-left: -100%;
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 200%;
                        height: 100%;
                        cursor: pointer;
                        opacity: 0;
                    }
                }
            }
            .header-right {
                display: flex;
                .el-button {
                    margin-left: 20px;
                }
            }
        }
        .curse-add {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
    .el-autocomplete {
        width: 350px;
    }
    .el-dialogAdd {
        transition: all 1s;
    }
    .el-tag {
        margin: 0 15px 15px 0;
    }
    .zd-fans {
        cursor: pointer;
    }

    .cell-title {
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333333;
        margin-bottom: 12px;
        display: flex;
        align-items: center;

        .user-name {
            max-width: 124px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .user-tags {
            display: flex;
            align-items: center;
            margin-left: 6px;

            .tag-item {
                margin-right: 4px;
                display: flex;
                align-items: center;

                img {
                    height: 14px;
                }
            }
        }
    }

    .cell-content {
        display: -webkit-box;
        -webkit-box-orient: vertical;
        overflow: hidden;
        -webkit-line-clamp: 3;
    }

    .cell-tips {
        color: #999;
    }
</style>
<style lang="less">
    .containerStudent .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .containerStudent .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .containerStudent .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .containerStudent .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>
